package com.keyancompany.zhongbaopingtai.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.IFragmentPagerAdapter;
import com.keyancompany.zhongbaopingtai.adapter.DiscussAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.fragment.DiscussFragment;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.LoadTalk;
import com.keyancompany.zhongbaopingtai.model.TalkList;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindString;

/**
 * 谈论区域
 */
public class DiscussAreaActivity extends BaseActivity {

	@BindString(R.string.discussion_area)
	String title;

	@Bind(R.id.toolbar)
	Toolbar toolbar;

	@Bind(R.id.tabLayout)
	TabLayout tabLayout;

	@Bind(R.id.complaint_viewpager)
	ViewPager complaintViewpager;

	private DiscussAdapter adapter;

	private List<TalkList> list;

	public List<TalkList> getList() {
		return list;
	}

	public void setList(List<TalkList> list) {
		this.list = list;
	}

	@Override
	protected int getContentViewId() {
		return R.layout.layout_tab_viewpager;
	}

	@Override
	protected int getMenuId() {
		return 0;
	}

	@Override
	protected void initToolBar() {
		initToolBar(toolbar, title);
	}

	@Override
	protected void setUpViews() {
		initData();
	}

	private void initTab() {

		List<Fragment> tcf = new ArrayList<>();
		tcf.add(new DiscussFragment().newInstance(2));
		tcf.add(new DiscussFragment().newInstance(2));
		IFragmentPagerAdapter pagerAdapter = new IFragmentPagerAdapter(getSupportFragmentManager(), tcf);

		String tabTitles[] = new String[]{getString(R.string.nice_post), getString(R.string.personal_post)};
		pagerAdapter.setTitles(tabTitles);
		complaintViewpager.setAdapter(pagerAdapter);
		tabLayout.setupWithViewPager(complaintViewpager);
		tabLayout.setTabMode(TabLayout.MODE_FIXED);
	}

	private void initData() {
		if (isNetvaiable()) {
			String teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);
			int pagenombers = 1;
			TeamAPI.loadTalk(teamCode, pagenombers, DiscussAreaActivity.this,
				getLoading(R.string.watting_hint));
		}
	}

	@Override
	public void success(int type, String json) {
		super.success(type, json);
		if (validateJson(json) != null) {
			BaseModel base = JSONObject.parseObject(json, BaseModel.class);
			LoadTalk loadtalk = JSONObject.parseObject(base.data, LoadTalk.class);

			if (base.result.equals(Constants.SUCESS)) {
				if (loadtalk.getPager().getList().size() > 0) {
					list = loadtalk.getPager().getList();
					initTab();
				} else {
					T.showShort(mContext, "当前没有讨论信息");
					return;
				}
			} else {
				T.showShort(mContext, base.msg);
			}
		}
	}

	@Override
	public void failed(String error) {
		super.failed(error);
		T.showShort(mContext, error);
	}
}
